<?php

declare(strict_types=1);

namespace app\api\controller\User;

use app\BaseController;
use app\common\model\User\Address as AddressModel;
use app\common\model\User\User as UserModel;
use app\common\model\Region as RegionModel;
use app\common\validate\User\Address as AddressValidate;
use think\exception\ValidateException;

class Address extends BaseController
{
    // 收货地址列表
    public function index()
    {
        $userid = $this->request->param('userid');
        $AddressList = AddressModel::where(['userid' => $userid])->select();

        if (!$AddressList) {
            $this->error('暂无收货地址');
        }

        $this->success('查询成功', null, $AddressList);
    }
    // 新增收货地址
    public function add()
    {
        try {
            $params = $this->request->param();

            $User = UserModel::find($params['userid']);

            if (!$User) {
                throw new \Exception("用户不存在");
            }

            $data = [
                'consignee' => $params['consignee'],
                'mobile' => $params['mobile'],
                'address' => $params['address'],
                'userid' => $params['userid']
            ];

            if (isset($params['status']) && $params['status'] === 1) {
                AddressModel::update(['status' => 0], ['userid' => $params['userid']]);

                $data['status'] = 1;
            }

            if (isset($params['code'])) {
                $parentpath = RegionModel::where(['code' => $params['code']])->value('parentpath');

                if (!$parentpath) {
                    throw new \Exception('所选地区不存在');
                }

                [$province, $city, $district] = explode(',', $parentpath);

                $data['province'] = $province;

                $data['city'] = $city;

                $data['district'] = $district;
            }

            validate(AddressValidate::class)->check($data);

            $result = AddressModel::create($data);

            if ($result === FALSE) {
                throw new \Exception('新增收货地址失败');
            }
        } catch (ValidateException $e) {
            $this->error($e->getError());
        } catch (\Exception $error) {
            $this->error($error->getMessage());
        }
        $this->success('新增收货地址成功');
    }
    // 设置默认收货地址
    public function select()
    {
        $userid = $this->request->param('userid');

        $id = $this->request->param('id');

        $User = UserModel::find($userid);

        if (!$User) {
            $this->error('用户不存在');
        }

        $address = AddressModel::find($id);

        if (!$address) {
            $this->error('所选的收货地址不存在');
        }

        AddressModel::update(['status' => 0], ['userid' => $userid]);

        $result = AddressModel::update(['status' => 1], ['id' => $id]);

        if ($result !== FALSE) {
            $this->success('设置默认收货地址成功');
        } else {
            $this->error('设置默认收货地址失败');
        }
    }
    // 收货地址详情
    public function info()
    {
        $addressid = $this->request->param('addressid');

        $address = AddressModel::find($addressid);

        if (!$address) {
            $this->error('所选的收货地址不存在');
        }

        $this->success('查询成功', null, $address);
    }
    // 编辑收货地址
    public function edit()
    {
        try {
            $params = $this->request->param();
            $Address = AddressModel::find($params['addressid']);

            if (!$Address) {
                throw new \Exception("收货地址不存在");
            }

            $data = [
                'consignee' => $params['consignee'],
                'mobile' => $params['mobile'],
                'address' => $params['address'],
            ];

            if (isset($params['status'])) {
                if ($params['status'] === 1) {
                    AddressModel::update(['status' => 0], ['userid' => $params['userid']]);
                    $data['status'] = 1;
                }
                if($params['status'] ==0){
                    AddressModel::update(['status' => 0], ['id' => $params['addressid']]);
                    $data['status'] = 0;
                }
            }

            if (isset($params['code'])) {
                $parentpath = RegionModel::where(['code' => $params['code']])->value('parentpath');

                if (!$parentpath) {
                    throw new \Exception('所选地区不存在');
                }

                [$province, $city, $district] = explode(',', $parentpath);

                $data['province'] = $province;

                $data['city'] = $city;

                $data['district'] = $district;
            }

            validate(AddressValidate::class)->scene('edit')->check($data);

            $result = AddressModel::update($data, ['id' => $params['addressid']]);

            if ($result === FALSE) {
                throw new \Exception('修改收货地址失败');
            }
        } catch (ValidateException $e) {
            $this->error($e->getError());
        } catch (\Exception $error) {
            $this->error($error->getMessage());
        }
        $this->success('修改收货地址成功');
    }
    // 删除收货地址
    public function delete()
    {
        $addressid = $this->request->param('addressid');
        $address = AddressModel::find($addressid);
        if (!$address) {
            $this->error('所选的收货地址不存在');
        }
        $result = AddressModel::where(['id' => $addressid])->delete();
        if ($result) {
            $this->success('删除收货地址成功');
        } else {
            $this->error('删除收货地址失败');
        }
    }
    // 订单结算获取默认收货地址
    public function order()
    {
        $userid = $this->request->param('userid');

        $User = UserModel::find($userid);

        if (!$User) {
            $this->error('用户不存在');
        }

        $address = AddressModel::where(['userid' => $userid, 'status' => 1])->find();

        if ($address) {
            $this->success('查询成功', null, $address);
        } else {
            $this->error('暂无默认收货地址');
        }
    }
}
